package com.vanguard.controller;

import com.vanguard.commons.base.BaseController;
import com.vanguard.commons.base.BaseDomain;
import com.vanguard.commons.constant.CommonReturnCode;
import com.vanguard.commons.pojo.ResponseResult;
import com.vanguard.commons.utils.CookieUtils;
import com.vanguard.commons.utils.LoggerUtils;
import com.vanguard.domain.User;
import com.vanguard.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Title: 微信认证登录相关前端控制器
 * @Description:
 * @Author: vanguard
 * @Version: 1.0
 * @Date: 2018/11/16
 */
@Controller
@RequestMapping("/weixin")
public class WeixinController extends BaseController {

    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/auth", method = RequestMethod.GET)
    public Object auth(@RequestParam("code") String code, @RequestParam("state") String state) {

        LoggerUtils.debug(WeixinController.class, "code" + code);
        LoggerUtils.debug(WeixinController.class, "state" + code);
        String openId = userService.getOpenId(code, state);
        //将openId存入Cookie中
        CookieUtils.setCookie(getRequest(), getResponse(), "openId", openId, 60*60*24*7);
        //重定向到预约项目页面
        return redirectTo(getRequest().getContextPath() + "/appoint/add");
    }
}
